package com.lin.zoo.view;

import com.lin.zoo.po.*;
import com.lin.zoo.controller.UserController;
import com.lin.zoo.dao.CookieDao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class StartPage extends JFrame {

    private LoginPage loginPage = new LoginPage();

    public static void main(String[] args) {
        new StartPage("主页面").setVisible(true);
    }

    public StartPage(String title) throws HeadlessException {
        super(title);
        setSize(550, 330);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setResizable(false);
        //固定窗口大小不被修改
        setLocationRelativeTo(null);
        //窗口处于中央

        loginPage.getBtnLogin().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String userName = loginPage.getJtaText().getText();
                String password = loginPage.getJtbText().getText();
                //获取数据
                new CookieDao().setcookie(userName,password);
                loginPage.setUserController(new UserController());
                Messege messege = loginPage.getUserController().login(userName, password);

                //将获取的数据返回到service
                if (messege.getResult().equals("登录成功")) {

                    System.out.println(messege);
                    remove(loginPage);
                    getContentPane().add(new Result(messege.getResult()));
                    getContentPane().validate();
                    JOptionPane.showMessageDialog(null, messege.getResult(), null, JOptionPane.PLAIN_MESSAGE);

                    //登陆成功
                    Messege user = loginPage.getUserController().disuser();

                    System.out.println(user);
                    remove(loginPage);
                    getContentPane().add(new Result(user.getResult()));
                    getContentPane().validate();
                    JOptionPane.showMessageDialog(null, user.getResult(), null, JOptionPane.PLAIN_MESSAGE);
                    //确认用户身份
                    switch (user.getResult()) {
                        case "游客身份登录":
                            VisitorPage visitorPage = new VisitorPage();
                            visitorPage.createAndShowGUI();

                            //点击查询用户信息
                            visitorPage.getVInformation().addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    remove(visitorPage);
                                    JOptionPane.showMessageDialog(null, user.getMessage(), "用户信息页面", JOptionPane.PLAIN_MESSAGE);
                                }
                            });
                            //点击查询园区信息
                            visitorPage.getVPark().addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    remove(visitorPage);
                                    ParkPage parkPage = new ParkPage();
                                    parkPage.createAndShowGUI();
                                }
                            });
                            //修改个人信息
                            visitorPage.getReInformation().addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    remove(visitorPage);
                                    ReinformationPage reinformationPage = new ReinformationPage();
                                    reinformationPage.createAndShowGUI();
                                }
                            });
                            //门票订单系统
                            visitorPage.getIndent().addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    remove(visitorPage);
                                    IndentVisitorPage indentVisitorPage = new IndentVisitorPage();
                                    try {
                                        indentVisitorPage.createAndShowGUI();
                                    } catch (SQLException ex) {
                                        System.out.println("界面打开错误" + this.getClass().getName());
                                    }
                                }
                            });

                            //查看活动与评论
                            visitorPage.getActivityAndComment().addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    remove(visitorPage);
                                    ParkVisitorPage parkVisitorPage = new ParkVisitorPage();
                                    try {
                                        parkVisitorPage.createAndShowGUI();
                                    } catch (SQLException ex) {
                                        System.out.println("界面打开错误" + this.getClass().getName());
                                    }
                                }
                            });

                            break;
                        case "园长身份登录":
                            MasterPage masterPage = new MasterPage();
                            masterPage.createAndShowGUI();

                            break;
                        case "饲养员身份登录":
                            FeederPage feederPage = new FeederPage();
                            feederPage.createAndShowGUI();

                            feederPage.getVPark().addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    remove(feederPage);
                                    ParkPage parkPage = new ParkPage();
                                    parkPage.createAndShowGUI();
                                }
                            });

                            feederPage.getRePark().addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    ParkMasterPage masterPage = new ParkMasterPage();
                                    try {
                                        masterPage.createAndShowGUI();
                                    } catch (SQLException ex) {
                                        System.out.println("界面打开错误" + this.getClass().getName());
                                    }
                                }
                            });

                            feederPage.getReAnimal().addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    AnimalFeederPage animalFeederPage = new AnimalFeederPage();
                                    try {
                                        animalFeederPage.createAndShowGUI();
                                    } catch (SQLException ex) {
                                        System.out.println("界面打开错误" + this.getClass().getName());
                                    }
                                }
                            });

                            feederPage.getReActivity().addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    ActivityFeederPage activityFeederPage = new ActivityFeederPage();
                                    try {
                                        activityFeederPage.createAndShowGUI();
                                    } catch (SQLException ex) {
                                        System.out.println("界面打开错误" + this.getClass().getName());
                                    }
                                }
                            });
                            break;
                    }
                } else {
                    JOptionPane.showMessageDialog(null, messege.getResult(), null, JOptionPane.ERROR_MESSAGE, null);
                    loginPage.getJtaText().setText("");
                    loginPage.getJtbText().setText("");
                }
            }
        });

        getContentPane().add(loginPage);


        loginPage.getBtnSign().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String userName = loginPage.getJtaText().getText();
                String password = loginPage.getJtbText().getText();
                //获取输入的信息

                loginPage.setUserController(new UserController());
                Messege messege = loginPage.getUserController().sign(userName, password);
                if (messege.getResult().equals("注册成功")) {
                    setVisible(false);
                    System.out.println(messege);
                    remove(loginPage);
                    getContentPane().add(new Result(messege.getResult()));
                    getContentPane().validate();
                    JOptionPane.showMessageDialog(null, messege.getResult(), null, JOptionPane.PLAIN_MESSAGE);
                    loginPage.getJtaText().setText("");
                    loginPage.getJtbText().setText("");

                    SignPage signPage=new SignPage();
                    signPage.createAndShowGUI();

                }
            }
        });

    }
}
